package com.yj.cloudnote.controller;

import com.yj.cloudnote.service.FilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@RequestMapping(value = "/file")
@Controller
public class FileController {
    @Autowired
    private FilesService filesService;

    /**
     * @Author: jy
     * @Description:
     * @Date: 19:27 2018/4/27
     */
    @RequestMapping(value = "/addFile")
    @ResponseBody
    public void addNewFile(@RequestBody Map<String,Object> params) {
        String username = (String) params.get("username");
        int fileId = (int) params.get("file_id");
        filesService.addNewFile(username,fileId);
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 21:42 2018/4/27
     */
    @RequestMapping(value = "/updataFile")
    @ResponseBody
    public Map<String,Object> updataFile(@RequestBody Map<String,Object> params) {
        String username = (String) params.get("username");
        int fileId = (int) params.get("file_id");
        String content = (String) params.get("content");
        if (StringUtils.isEmpty(content)) {
            content = "";
        }
        filesService.updataFile(username,fileId,content);
        Map<String,Object> res = new HashMap<>();
        res.put("isSave",true);
        return res;
    }

    @RequestMapping(value = "/getFileContent")
    @ResponseBody
    public Map<String,Object> getFileContent(@RequestBody Map<String,Object> params) {
        String username = (String) params.get("username");
        int fileId = (int) params.get("file_id");
        String content = filesService.getFileContent(username,fileId);
        Map<String,Object> res = new HashMap<>();
        res.put("content",content);
        return res;
    }
}
